var max = 8;
var current = 4;
function addMoreRows(){	
	if (current < max){
		current = current + 1;
		
		for (var i = current; i <= current; i++){

			var row = '<tr><td><input id="file_' + i + '" type="file" name="file_' + i + '" placeholder="FileAttached" /></td>'
			+'<td><select id="type_' + i + '" type="text" name="type_' + i + '">'

		   +'<option value="ItemData">ItemData</option>'
        +'<option value="ItemGridData">ItemGridData</option>'
        +'<option value="TimeSeriesData">TimeSeriesData</option>'
        +'<option value="HeatMapData">HeatMapData</option>'
        +'<option value="GraphData">GraphData</option>'
        +'<option value="TimedItemData">TimedItemData</option>'
	+'</select></td>'
	+'<td><select id="cs_'+i+'" type="text" name="cs_'+i+'">'
	+'<option value="parula">parula</option>'
	+'<option value="jet">jet</option>'
	+'<option value="hsv">hsv</option>'
	+'<option value="hot">hot</option>'
	+'<option value="cool">cool</option>'
	+'<option value="spring">spring</option>'
	+'<option value="summer">summer</option>'
	+'<option value="autumn">autumn</option>'
	+'<option value="winter">winter</option>'
	+'<option value="gray">gray</option>'
	+'<option value="bone">bone</option>'
	+'<option value="copper">copper</option>'
	+'<option value="pink">pink</option>'
	+'<option selected value="bluered">bluered</option>'
	+'<option value="whitered">whitered</option>'
	+'</select></td>'
			+'<td style="width: 200px"><input id="desc_' + i + '" type="text" name="desc_' + i + '" /></td>'
			+'</tr>';
			$("#mnfiles").append(row);
		}
			
	}else{
		alert("Sorry, maximum of 9 input files are allowed");
	}
}

$(document).ready(function () {
    toggleFields();
  //call this first so we start out with the correct visibility depending on the selected form values
    //this will call our toggleFields function every time the selection value of our underAge field changes

    for (i = 0; i <= current ; i++)
	{
	    field = "#type_" + i;
	    $(field).change(function () {
        	toggleFields();
	    });
	}

});

function toggleFields() {

	for(i=0; i <= current; i++)
		{
		if($("#type_"+ i ).val() == "GraphData")
		{
			$("#csdiv_"+i).hide();
		}
		else
		{
			$("#csdiv_"+ i).show();	
		}	
	}
}

function validate(){
	var name = $("#name").val();
	var email = $("#email").val();
	if (name == "" || email == ""){
		alert("Name and emails fields are required");
		return false;
	}	
	var allefiles = true;
	for (var i = 0; i < max; i++){
		var file_selected = $("#file_"+i).val();
		if (file_selected != null && file_selected != ""){
			allefiles = false;
		}
	}
	if (allefiles == true){
		alert("Zero Input files selected");
		return false;
	}
	return true;
}

//Callback handler for form submit event
function submit_job(){
  if (!validate()){
	new Messi('Validation failed', {titleClass:'warning' ,title: 'Validation'});
        return;
  }else{
  //$("#resultsButton").prop("disabled",true);
  $("#fmsg").html("Uploading/processing ... <img src='images/wait.gif' />");
  $("#resultsform").submit(function(e) {
    $("#resultsButton").prop("disabled",true);
    var formObj = $(this);
    var formURL = "/cgi-bin/init_idare.cgi";
    var formData = new FormData(this);
    formData.append("inmax", current);
    //var imageformat = $('input[name=imageformat]:checked', '#resultsform').val();
    //formData.append("imageformat", imageformat);
    $.ajax({
        url: formURL,
        type: 'POST',
        data:  formData,
        mimeType:"multipart/form-data",
        contentType: false,
        cache: false,
        processData:false,
    success: function(data, textStatus, jqXHR)
    {
        $("#resultsButton").prop("disabled",false);
      $("#fmsg").html("Job submitted");
      	//$("#resultsmsg").html("<font color='red'>Thanks, form saved and email sent</font>");
        new Messi('Job submitted, status will be posted via email', {titleClass:'success',title: 'Success'});
    },
     error: function(jqXHR, textStatus, errorThrown)
     {
	$("#resultsButton").prop("disabled",false);
	$("#fmsg").html("Server error on submit, could be network problem and if problem persists, please inform administrator");
       new Messi("Server error on submit, could be network problem and if problem persists, please inform administrator");
     }   
    });
    e.preventDefault(); //Prevent Default action.
    //e.unbind();
    });//end of submit
    }
}

function linkref(){
    new Messi('User guide under construction', {titleClass:'warning' ,title: 'User guide'});     
}

